package com.ruoyi.wx.service.impl;

import java.util.Date;
import java.util.List;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wx.domain.WxApp;
import com.ruoyi.wx.mapper.WxAppMapper;
import com.ruoyi.wx.service.IWxAppService;

/**
 * 小程序应用Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-29
 */
@Service
public class WxAppServiceImpl extends ServiceImpl<WxAppMapper, WxApp> implements IWxAppService {
    
	@Autowired
    private WxAppMapper wxAppMapper;

    /**
     * 查询小程序应用
     * 
     * @param id 小程序应用主键
     * @return 小程序应用
     */
    @Override
    public WxApp selectWxAppById(String id)
    {
        return wxAppMapper.selectWxAppById(id);
    }

    /**
     * 查询小程序应用列表
     * 
     * @param wxApp 小程序应用
     * @return 小程序应用
     */
    @Override
    public List<WxApp> selectWxAppList(WxApp wxApp)
    {
        return wxAppMapper.selectWxAppList(wxApp);
    }

    /**
     * 新增小程序应用
     * 
     * @param wxApp 小程序应用
     * @return 结果
     */
    @Override
    public int insertWxApp(WxApp wxApp)
    {
        wxApp.setCreateTime(DateUtils.getNowDate());
        return wxAppMapper.insertWxApp(wxApp);
    }

    /**
     * 修改小程序应用
     * 
     * @param wxApp 小程序应用
     * @return 结果
     */
    @Override
    public int updateWxApp(WxApp wxApp)
    {
        wxApp.setUpdateTime(DateUtils.getNowDate());
        return wxAppMapper.updateWxApp(wxApp);
    }

    /**
     * 批量删除小程序应用
     * 
     * @param ids 需要删除的小程序应用主键
     * @return 结果
     */
    @Override
    public int deleteWxAppByIds(String[] ids)
    {
        return wxAppMapper.deleteWxAppByIds(ids);
    }

    /**
     * 删除小程序应用信息
     * 
     * @param id 小程序应用主键
     * @return 结果
     */
    @Override
    public int deleteWxAppById(String id)
    {
        return wxAppMapper.deleteWxAppById(id);
    }

	@Override
	public WxMaConfig getAppConfig(String appId) {
		if(StringUtils.isBlank(appId)){
			return null;
		}
		QueryWrapper<WxApp> queryWrapper = new QueryWrapper<>();
		WxApp entity = new WxApp();
		entity.setAppId(appId);
		queryWrapper.setEntity(entity);
		WxApp app = baseMapper.selectOne(queryWrapper);
		if(app == null){
			return null;
		}
		WxMaDefaultConfigImpl config =new WxMaDefaultConfigImpl();
		config.setAppid(app.getAppId());
		config.setSecret(app.getAppSecret());
		return config;
	}

	@Override
	public WxPayConfig getPayConfig(String appId) {
		if(StringUtils.isBlank(appId)){
			return null;
		}
		QueryWrapper<WxApp> queryWrapper = new QueryWrapper<>();
		WxApp entity = new WxApp();
		entity.setAppId(appId);
		queryWrapper.setEntity(entity);
		WxApp app = baseMapper.selectOne(queryWrapper);
		if(app == null){
			return null;
		}
		WxPayConfig config = new WxPayConfig();
		config.setAppId(appId);
		config.setMchId(app.getMchId());
		config.setMchKey(app.getMchKey());
		if(StringUtils.isNotBlank(app.getKeyPath())){
			config.setKeyContent(getCertStream(app.getKeyPath()));
		}
		config.setKeyPath(app.getKeyPath());
		config.setTradeType(app.getTradeType());
		return config;
	}
	
	/**
     * 获取p12证书文件内容的字节数组
     * 在相对路径下需要获取p12证书的内容
     * @return
     */
    public byte[] getCertStream(String filePath) {
       return FileUtil.readBytes(filePath);
    }

	@Override
	public WxApp getByAppid(String appId) {
		QueryWrapper<WxApp> queryWrapper = new QueryWrapper<>();
		WxApp entity = new WxApp();
		entity.setAppId(appId);
		queryWrapper.setEntity(entity);
		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public void addWxApp(WxApp app) {
		QueryWrapper<WxApp> queryWrapper = new QueryWrapper<>();
		WxApp entity = new WxApp();
		entity.setAppId(app.getAppId());
		queryWrapper.setEntity(entity);
		WxApp wxApp = baseMapper.selectOne(queryWrapper);
		if(wxApp == null){
			Date expireTime = DateUtil.offsetDay(DateUtil.date(), 7);
			app.setExpireTime(expireTime);
			app.setCreateTime(DateUtil.date());
			baseMapper.insert(app);
		}else{
			wxApp.setAppName(app.getAppName());
			baseMapper.updateById(wxApp);
		}
	}

}
